package app.netant.galgame.hgamecn;

import cn.lily.com.db.sql.DBManager;
import cn.lily.com.db.sql.manager.DBOfflineManager;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.lang.number.MathUtil;

public class HgameAntRoot{
	
	
	private static String homeUrl = "https://www.2dfan.com/subjects";
	
	private static String pageUrl = "https://www.2dfan.com/subjects/page/";
	
	public static void analyse(int maxIdx){
		
		System.out.println("整体处理开始........");
		
		//断点续传
		resume();
		
		for(int i=1;i<=maxIdx;i++){
			
			String url = "";
			if(i == 1){
				url = homeUrl;
			}else{
				url = pageUrl + i;
			}
			
			System.out.println("第"+i+"页数据开始处理！");
			
			try {
				HgamecnAntPage page = new HgamecnAntPage(url);
				page.analyse();
			} catch (Exception e) {
				e.printStackTrace();
				getDBManager().executeUpdate("insert into ${schema}.LEO_HGAMECN_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
										+ "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+url+"','1') ");					
				continue;
			}
			
		}
		
		System.out.println("整体处理结束........");
		
	}
	
	public static DBManager getDBManager(){
		return DBOfflineManager.getDBManager();
	}
	
	/**
	 * 断点续传
	 */
	public static void resume(){
		
		System.out.println("断点续传处理开始........");
		
		getDBManager().executeUpdate("delete from ${schema}.LEO_HGAMECN_INFO "
				+ "where GAME_URL in "
				+ "(select EXCEP_URL from ${schema}.LEO_HGAMECN_EXCEP)");
		
		getDBManager().executeUpdate("delete from ${schema}.LEO_HGAMECN_EXCEP");
		
		System.out.println("断点续传处理结束........");
		
	}
	
	
	public static void main(String[] args) {
		
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		//全更新
		HgameAntRoot.analyse(149);
		
		//异常修正
		//SadBrotherAntRoot.exceptionExcute();
		
		
	}
}
